package apps.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.dao.Pagination;
import webframe.common.dao.SqlParams;
import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;
import apps.web.controller.base.MyBaseCommandController;
import apps.web.model.Apply;

public class StatClearSearchController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request, HttpServletResponse response, Object command) {
		Apply capply = (Apply) command;
		DetachedCriteria criteria = DetachedCriteria.forClass(Apply.class);
		ControllerHelper.addEqualRestrictions(criteria, "id", capply.getId());
		ControllerHelper.addLikeRestrictions(criteria, "applyCode", capply.getApplyCode());
		ControllerHelper.addLikeRestrictions(criteria, "exhName", capply.getExhName());
		ControllerHelper.addLikeRestrictions(criteria, "exhAddress", capply.getExhAddress());

		ControllerHelper.addBetweenRestrictions(criteria, "beginDate", FormatTransformHelper.string2Date(capply
				.getPreContactor()), FormatTransformHelper.string2Date(capply.getPreTelephone()));

		ControllerHelper.addEqualRestrictions(criteria, "entUserId", capply.getEntUserId());

		// //
		ControllerHelper.addEqualRestrictions(criteria, "delFlag", 0L);
		ControllerHelper.addEqualRestrictions(criteria, "applyFlag", 11L);

		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<Apply> page = ControllerHelper.queryPaginationWithSql(request, criteria);
		SqlParams sqlParams = page.getSqlParams(true);
		// /////////////
		long[] stat = getServiceContainer().getCheckBizService().findAllClearCount(sqlParams);
		for (Apply apply : page.getItems()) {
			long declCount = getServiceContainer().getCheckBizService().findDeclCountByApplyId(apply.getId());
			long goodsCount = getServiceContainer().getCheckBizService().findGoodsCountByApplyId(apply.getId());
			long anchorCount = getServiceContainer().getCheckBizService().findAnchorGoodsCountByApplyId(apply.getId());
			long clearedCount = getServiceContainer().getCheckBizService()
					.findClearedGoodsCountByApplyId(apply.getId());
			long clearingCount = anchorCount - clearedCount;
			apply.getMap().put("declCount", declCount);
			apply.getMap().put("goodsCount", goodsCount);
			apply.getMap().put("anchorCount", anchorCount);
			apply.getMap().put("clearedCount", clearedCount);
			apply.getMap().put("clearingCount", clearingCount);
		}

		map.put("page", page);
		map.put("capply", capply);
		map.put("stat", stat);
		return new ModelAndView("StatClear.jsp", "map", map);
	}
}
